package com.giscloud.limp.controller;

import java.util.Arrays;

import com.giscloud.commons.utils.StringUtils;
import com.giscloud.excel.utils.ExcelUtil;
import com.giscloud.limp.entity.vo.CaseInfoVO;
import com.giscloud.limp.utils.DocUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;

import com.giscloud.limp.entity.CaseFirstInstance;
import com.giscloud.limp.service.CaseFirstInstanceService;
import com.giscloud.limp.entity.dto.CaseFirstInstanceDTO;
import com.giscloud.datasource.page.PageInfo;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.giscloud.commons.model.Result;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;


/**
 * 一审案件信息(案件维护)
 *
 * @author giscloud
 * @email giscloud@giscloud.com
 * @date 2020-06-10 14:25:28
 */
@RestController
@RequestMapping("/limp/casefirstinstance")
@Log4j2
public class CaseFirstInstanceController {

    private final CaseFirstInstanceService caseFirstInstanceService;

    @Autowired
    public CaseFirstInstanceController(CaseFirstInstanceService caseFirstInstanceService) {
        this.caseFirstInstanceService = caseFirstInstanceService;
    }
    /**
     * 案件维护列表
     */
    @RequestMapping("/list")
    public Result<PageInfo<CaseFirstInstance>> list(@RequestBody CaseFirstInstanceDTO caseFirstInstanceDTO){
        PageInfo page = caseFirstInstanceService.queryPage(caseFirstInstanceDTO);
        return Result.success(page);
    }


    /**
     * 案件维护导出
     */
    @RequestMapping("/export")
    public void export(CaseFirstInstanceDTO caseFirstInstanceDTO, HttpServletResponse response) throws Exception{
        List<CaseFirstInstance> list = caseFirstInstanceService.exportList(caseFirstInstanceDTO);
        ExcelUtil.export2Web(response,"案件维护","sheet1",CaseFirstInstance.class,list);
    }

    /**
     * 案件详情（诉讼/关联）
     */
    @RequestMapping("/caseInfo")
    public Result<CaseInfoVO> caseInfo(@RequestBody CaseFirstInstanceDTO caseFirstInstanceDTO){
        return caseFirstInstanceService.caseInfo(caseFirstInstanceDTO);
    }



    /**
     * 一审保存
     */
    @RequestMapping("/save")
    public Result save(@RequestBody CaseFirstInstance caseFirstInstance){
        return caseFirstInstanceService.save(caseFirstInstance);
    }

    /**
     * 一审修改
     */
    @RequestMapping("/update")
    public Result update(@RequestBody CaseFirstInstance caseFirstInstance){
        return caseFirstInstanceService.updateById(caseFirstInstance);
    }

    /**
     * 一审删除(直接传数组对象即可，如：[1],[1,2]支持批量删除)
     */
    @RequestMapping("/delete")
    public Result delete(@RequestBody Integer[] caseIds){
        return caseFirstInstanceService.removeByIds(Arrays.asList(caseIds));
    }

    /**
     * 案件说明下载
     */
    @RequestMapping("/downloadCaseDesc")
    public Result downloadCaseDesc(CaseFirstInstanceDTO caseFirstInstanceDTO, HttpServletResponse response){
        if(caseFirstInstanceDTO == null || caseFirstInstanceDTO.getCaseId() == null){
            return Result.error(-1,"caseId 必填");
        }
        Map<String,String> map = caseFirstInstanceService.downloadCaseDesc(caseFirstInstanceDTO);
        if(map == null){
            return Result.error(-1,"此 caseId 无数据");
        }
        DocUtils.download(response,map.get("caseName")+"-案件说明.docx",map);
        return null;
    }

    /**
     * 诉讼标的额统计（按年）
     */
    @RequestMapping("/litigationAmountByYear")
    public Result litigationAmountByYear(@RequestBody Map<String,Object> params){
        if(params.get("createdOrgId") != null){
            params.put("createdOrgId",new BigDecimal(String.valueOf(params.get("createdOrgId"))));
        }
        params.put("dateformat","'yyyy'");
        return caseFirstInstanceService.litigationAmountByYear(params);
    }

    /**
     * 诉讼标的额统计(按月)
     */
    @RequestMapping("/litigationAmountByMonth")
    public Result litigationAmountByMonth(@RequestBody Map<String,Object> params){
        if(params.get("createdOrgId") != null){
            params.put("createdOrgId",new BigDecimal(String.valueOf(params.get("createdOrgId"))));
        }
        params.put("dateformat","'yyyymm'");
        return caseFirstInstanceService.litigationAmountByYear(params);
    }

    /**
     * 诉讼标的总额统计（按年）
     */
    @RequestMapping("/litigationTotalAmountByYear")
    public Result litigationTotalAmountByYear(@RequestBody Map<String,Object> params){
        if(params.get("createdOrgId") != null){
            params.put("createdOrgId",new BigDecimal(String.valueOf(params.get("createdOrgId"))));
        }
        params.put("dateformat","'yyyy'");
        return caseFirstInstanceService.litigationTotalAmountByYear(params);
    }

    /**
     * 诉讼标的总额统计(按月)
     */
    @RequestMapping("/litigationTotalAmountByMonth")
    public Result litigationTotalAmountByMonth(@RequestBody Map<String,Object> params){
        if(params.get("createdOrgId") != null){
            params.put("createdOrgId",new BigDecimal(String.valueOf(params.get("createdOrgId"))));
        }
        params.put("dateformat","'yyyymm'");
        return caseFirstInstanceService.litigationTotalAmountByYear(params);
    }

}
